<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class CarController extends Controller
{
     public function index(Request $request)
    {	
        // dd($request);
        // dd($id);
        // session中的homeuser如果存在直接进入购物车页否则显示购物车登陆页面
    	if ($request->session()->has('homeuser')) {
            $id = $request->session()->get('homeuser')->userid;
    		$list = DB::table('car')->where('userid','=',$id)->orderBy('carid','desc')->get();
    		// dd($list);
            return view('home.car.car',['list'=>$list,'userid'=>$id]);
    	}else{
            return view('home.car.carlogin');
    	}
    }
    // 处理购物车的登陆页面
     public function store(Request $request)
    {
        // dd($request);
        // dd($request->only('a'));
        $name = $request->input("user");
        // $pass = $request->input("pwd");
        $ob = DB::table("user")->where("username",$name)->first();
        if(empty($name)){
            return back()->with("msg","请输入账号");
        }
        if($ob){
            if($request->input("pwd")==$ob->userpassword){
                session(["homeuser"=>$ob]);
                // 登陆成功进入购物车页
                return redirect("/home/car");
            }else{
                // return back()->with("msg","登录失败：密码错误");
            }
        }else{
            return back()->with("msg","登录失败：账号不存在");
        }
        
    }
    //商品删除操作
	public function destroy($id,Request $request)
	{
		$m=DB::table('car')->where('carid',$id)->delete();		
		if ($m>0) {
    		return redirect('/home/car');
    	}
    }
    //加入购物车操作
    public function doAjax(Request $request)
    {
        $userid = $request->input('userid');
        $goodsid = $request->input('goodsid');
        $goodspic= $request->input('goodspic');
        $goodsname = $request->input('goodsname');
        $goodsprice = $request->input('price');
        $goodscolor = $request->input('color');
        $list = DB::table('goodsdet')->where('goodsdetid','=',$goodsid)->get();
        foreach ($list[0] as $k => $v) {
            if ($v==$goodsprice) {
               $id = $k[5];
            }
        }
        $vers = "version"."$id";
        $goodsversion = $list[0]->$vers;
        $data = array('userid'=>$userid,'goodspic'=>$goodspic,'goodsid'=>$goodsid,'goodsprice'=>$goodsprice,'goodsname'=>$goodsname,'goodsversion'=>$goodsversion,'goodscolor'=>$goodscolor);
        echo $m = DB::table('car')->insertGetId($data);
    }
    public function doEdit(Request $request)
    {
        $da = array_chunk($request->except('_token'),3);
        if ($da) {
            // dd($da); 
            foreach ($da as $k => $v) {
                $id = $v[1];
                $idd[] = $id;
                $data = array('prices'=>$v[0],'goodsnum'=>$v[2]);
                $m = DB::table('car')->where('carid','=',$id)->update($data);
            }
            session(["carid"=>$idd]);
            if($m>0){
                return redirect('home/txdd');
            }else{
                return back()->with('msg','生成订单失败!!!,请重新购买!!!');
            }
        }else{
           return back()->with('msg','请选择你要购买的商品和数量!!!'); 
        }
    }
}
